package com.example.smartagrisys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.entity.LandCropRecord;
import com.example.smartagrisys.mapper.LandCropRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 作物相关控制器
 */
@Slf4j
@RestController
@RequestMapping("/crop")
public class CropController {
    
    @Resource
    private LandCropRecordMapper landCropRecordMapper;
    
    /**
     * 获取作物类型列表
     * 从实际种植记录中获取作物类型
     */
    @GetMapping("/types")
    public Result<List<String>> getCropTypes() {
        // 从种植记录中获取所有作物类型
        LambdaQueryWrapper<LandCropRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(LandCropRecord::getCropName)
                   .groupBy(LandCropRecord::getCropName);
        
        List<LandCropRecord> records = landCropRecordMapper.selectList(queryWrapper);
        Set<String> cropNameSet = new HashSet<>();
        
        // 提取作物名称
        for (LandCropRecord record : records) {
            if (record.getCropName() != null && !record.getCropName().isEmpty()) {
                cropNameSet.add(record.getCropName());
            }
        }
        
        List<String> cropTypes = new ArrayList<>(cropNameSet);
        log.info("获取作物类型列表，共{}个类型: {}", cropTypes.size(), cropTypes);
        return Result.success(cropTypes);
    }
} 